//
//  LocaleDateVerifyClass.m
//  Growth
//
//  Created by 洪波 陈 on 7/6/12.
//  Copyright (c) 2012 MEETStudio. All rights reserved.
//

#import "LocaleDateVerifyClass.h"
#import "WebRequest.h"
#import "PublicMethodEngine.h"

static LocaleDateVerifyClass *_localeDateVerifyClass = nil;

@implementation LocaleDateVerifyClass

+ (id)shareLocaleDateVerifyClass
{
    if (!_localeDateVerifyClass) {
        _localeDateVerifyClass = [[LocaleDateVerifyClass alloc] init];
    }
    
    return _localeDateVerifyClass;
}


- (void)verifyMethod
{
    NSInteger verifyInterval = VERIFY_LOCALE_DATE_INTERVAL;
    
    CLog(@"VERIFY_LOCALE_DATE_INTERVAL %d", verifyInterval);
    
    NSMutableURLRequest *request = [WebRequest systemDate];
    CLog(@"request %@", request);
    
    [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    
    NSDate *zoneDate = [NSDate date];
    NSTimeZone *zone = [NSTimeZone defaultTimeZone];//获得当前应用程序默认的时区
    NSInteger interval = [zone secondsFromGMTForDate:zoneDate];//以秒为单位返回当前应用程序与世界标准时间（格林威尼时间）的时差
    NSDate *nd=[NSDate dateWithTimeIntervalSinceNow:interval];
    CLog(@"LocaleDateVerifyClass %@", nd);
}


#pragma mark -

// NSURLConnection Delegate Methods. You would want to include more for error handling //
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    
    NSHTTPURLResponse  *httpResponse = ( NSHTTPURLResponse  *)response;
    
    statusCode = [httpResponse  statusCode ];
    
    CLog( @"status code = %d" ,statusCode);

//    NSHTTPURLResponse  *httpResponse = ( NSHTTPURLResponse  *)response;
//	NSDictionary *headerFields = [response allHeaderFields]; 
//    CLog(@"LocaleDateVerifyClass headerFields %@", headerFields);
//    
//    NSDate *date = [headerFields objectForKey:@"Date"];
//    
//    CLog(@"LocaleDateVerifyClass date %@", date);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSMutableData *)data {
	CLog(@"Recieving Data...");
    CLog(@"LocaleDateVerifyClass Receive data %@", data);
    
    if (data != nil  && statusCode == 200) {
        NSString *dateStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        long long int serviceInterval = [dateStr longLongValue];
        
        NSDate *zoneDate = [NSDate date];
        NSTimeZone *zone = [NSTimeZone defaultTimeZone];//获得当前应用程序默认的时区
        NSInteger interval = [zone secondsFromGMTForDate:zoneDate];//以秒为单位返回当前应用程序与世界标准时间（格林威尼时间）的时差
        
        NSDate *nd=[NSDate dateWithTimeIntervalSinceNow:interval];
        NSTimeInterval _interval = [nd timeIntervalSince1970];

        serviceInterval = serviceInterval/1000 +interval;
        
        //日志时间
        NSDate *serviceDate = [NSDate dateWithTimeIntervalSince1970:serviceInterval];
        CLog(@"\nService date %@, \nLocale date %@", serviceDate, nd);
        
        NSInteger difference = serviceInterval - _interval;
        
        CLog(@"Service interval %lld, interal %f, difference %d", serviceInterval, _interval , difference);
        
        
        [[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%d", difference] forKey:VERIFY_LOCALE_DATE_KEY];
        
        CLog(@"VERIFY_LOCALE_DATE_INTERVAL %d", VERIFY_LOCALE_DATE_INTERVAL);
    }
    

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
	
    CLog(@"LocaleDateVerifyClass error %@", error);
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	CLog(@"Fininshed Loading...");
    
    CLog(@"LocaleDateVerifyClass Finish loading %@", connection);
}

@end
